"""
Case Type   : borrow_memory
Case Name   : 设置max_imcs_cache*htap_borrow_mem_percent>max_borrow_memory，合理提示
Create At   : 2025/4/15
Owner       : @li-xin12345
Description :
    1、设置相关参数有效值
    htap_borrow_mem_percent使用默认值
    max_imcs_cache 200MB
    lmemfabric_client_path依赖rackmanager,当前使用默认值，不影响参数设置
    alter system set htap_borrow_mem_percent to '128MB';
    2、alter system set设置htap_borrow_mem_percent为99
    3、gs_guc set设置htap_borrow_mem_percent为100
    4、清理环境
Expect      :
    1、成功
    2、合理提示(无内存借用环境未触发提示)
    3、合理提示(无内存借用环境未触发提示)
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BorrowMemoryTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_db_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.param_names = ['htap_borrow_mem_percent', 'max_imcs_cache',
                            'max_borrow_memory', 'lmemfabric_client_path']
        self.default_values = [self.common.show_param(i) for i in
                               self.param_names]

    def test_borrow_memory(self):
        text = '-----step1:设置相关参数有效值;expect:成功-----'
        self.log.info(text)
        res = self.pri_db_sh.execut_db_sql(
            f'alter system set {self.param_names[2]} to \'128MB\';')
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg, res,
                      '执行失败：' + text)

        res = self.pri_db_sh.execut_db_sql(
            f'alter system set {self.param_names[1]} to \'200MB\';')
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg, res,
                      '执行失败：' + text)

        res = self.pri_db_sh.restart_db_cluster()
        self.log.info(res)
        status = self.pri_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)
        res = [self.common.show_param(i) for i in self.param_names[1:3]]
        self.log.info(res)
        self.assertIn('128MB', res, '执行失败：' + text)
        self.assertIn('200MB', res, '执行失败：' + text)

        text = ('-----step2:alter system set设置htap_borrow_mem_percent为99;'
                'expect:合理提示-----')
        self.log.info(text)
        res = self.pri_db_sh.execut_db_sql(
            f'alter system set {self.param_names[0]} to 99;')
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg, res,
                      '执行失败：' + text)

        res = self.pri_db_sh.restart_db_cluster(get_detail=True)
        self.log.info(res)

        status = self.pri_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)

        res = self.common.show_param(self.param_names[0])
        self.log.info(res)
        self.assertIn('99', res, '执行失败：' + text)

        text = ('-----step3:gs_guc set设置htap_borrow_mem_percent为100;'
                'expect:合理提示-----')
        self.log.info(text)
        res = self.pri_db_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'{self.param_names[0]}=100', get_detail=True)
        self.log.info(res)
        self.assertTrue(res, '执行失败：' + text)

        res = self.pri_db_sh.restart_db_cluster(get_detail=True)
        self.log.info(res)

        status = self.pri_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)

        res = self.common.show_param(self.param_names[0])
        self.log.info(res)
        self.assertIn('100', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:清理环境; expect:成功-----'
        self.log.info(text)
        current_values = [self.common.show_param(i) for i in self.param_names]
        self.log.info(current_values)
        reset_flag = False
        if current_values != self.default_values:
            for param_name, current_val, default_val in zip(
                    self.param_names, current_values, self.default_values):
                if current_val != default_val:
                    set_res = self.pri_db_sh.execute_gsguc(
                        'set', self.constant.GSGUC_SUCCESS_MSG,
                        f'{param_name}={default_val}')
                    self.log.info(set_res)
                    self.assertTrue(set_res, '执行失败：' + text)
                    reset_flag = True
        if reset_flag:
            res = self.pri_db_sh.restart_db_cluster()
            self.log.info(res)
            status = self.pri_db_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in status)
            res = [self.common.show_param(i) for i in self.param_names]
            self.log.info(res)
            self.assertEqual(self.default_values, res, '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')